package com.tang.crm.controller;

import com.tang.crm.base.BaseController;
import com.tang.crm.base.ResultInfo;
import com.tang.crm.dto.CusDevPlanDto;
import com.tang.crm.entity.CusDevPlan;
import com.tang.crm.entity.SaleChance;
import com.tang.crm.service.CusDevPlanService;
import com.tang.crm.service.SaleChanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 客户计划管理
 * @author SHIRELY阳
 */
@Controller
@RequestMapping("/cus_dev_plan")
public class CusDevPlanController extends BaseController {

    @Autowired
    private SaleChanceService saleChanceService;

    @Autowired
    private CusDevPlanService cusDevPlanService;

    /**
     * 跳转到客户计划表的主页
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return "cusDevPlan/cus_dev_plan";
    }

    /**
     * 打开计划项开发与详情页面
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/toCusDevPlanPage")
    public String toCusDevPlanPage(Integer id , HttpServletRequest request){
        SaleChance saleChance = saleChanceService.getById(id);
        request.setAttribute("saleChance", saleChance);
        return "cusDevPlan/cus_dev_plan_data";
    }

    /**
     * 客户开发机会数据查询(多条件)
     * @param cusDevPlanDto
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> queryCusDevPlanByParams(CusDevPlanDto cusDevPlanDto){
        return cusDevPlanService.queryCusDevByParams(cusDevPlanDto);
    }

    /**
     * 添加计划项
     * @param cusDevPlan
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultInfo insertCusDevPlan(CusDevPlan cusDevPlan){
        cusDevPlanService.insertCusDevPlan(cusDevPlan);
        return success("计划项数据添加成功");
    }

    /**
     * 更新计划项
     * @param cusDevPlan
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultInfo updateCusDevPlan(CusDevPlan cusDevPlan){
        cusDevPlanService.updateCusDevPlan(cusDevPlan);
        return success("计划项数据更新成功");
    }

    /**
     * 删除计划项
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResultInfo deleteCusDevPlan(Integer id){
        cusDevPlanService.deleteCusDevPlan(id);
        return success("计划项数据删除成功");
    }

    /**
     * 进入客户营销机会数据的更新/修改页面
     * @param request
     * @param sId
     * @param id
     * @return
     */
    @RequestMapping("toAddOrUpdateCusDevPlanPage")
    public String toAddOrUpdateCusDevPlanPage(HttpServletRequest request, Integer sId, Integer id){
        // 将营销机会ID设置到请求域中，给计划项页面获取
        request.setAttribute("sId", sId);
        // 通过计划项ID查询记录
        CusDevPlan cusDevPlan = cusDevPlanService.getById(id);
        // 将计划项数据设置到请求域中
        request.setAttribute("cusDevPlan", cusDevPlan);
        return "cusDevPlan/add_update";
    }
}
